const Users = require('../db/users');

module.exports = {
    // 注册
    async register(req, res, next) {
        let data = req.body;
        if (!data.phone || !data.password)
            return res.send('invalid phone or password');

        res.send((await (new Users(req.body)).save()));
    },

    // 登录 
    async login(req, res, next) {
        let data = req.body;
        if (!data.phone || !data.password)
            return res.send('invalid phone or password');
        let user = await Users.findOne(data).exec();

        res.send(user ? user : '此账号未注册，请先去注册！');
    },
    // 获取List
    async list(req, res, next) {
        res.send((await Users.find().select('_id name phone password createdAt updatedAt').exec()));
    }
}